<?php

namespace App\System\Cis\Repositories;

use App\Module\Api\Controllers\Request\AddressSubmitRequest;
use App\Module\Api\Controllers\Request\TableRequest;
use App\System\Base\Helpers\Traits\SinglePattern;
use App\System\Base\Repositories\BaseRegionRepository;
use App\System\Base\Repositories\Repository;
use App\System\Cis\Model\CisUserAddress;


class CisUserAddressRepository extends Repository
{
    use SinglePattern;


    /**
     * API 添加编辑收货地址信息
     * @param AddressSubmitRequest $addressSubmitRequest
     * @param $user_id 用户ID
     * @return array
     */
    public function editData(AddressSubmitRequest $addressSubmitRequest,$user_id)
    {
        $address = $addressSubmitRequest->address;
        $id = isset($address['id']) ? $address['id'] : '';
        $region = BaseRegionRepository::getInstance();
        $set = ['consignee'=>$address['name'],'mobile'=>$address['mobile'],'is_default'=>$address['isDefault'],'street'=>$address['street'],'zipcode'=>0];
        $region_array = $region->getOne($address['regionId']);
        $key = ['0'=>'region_id','1'=>'city_id','2'=>'province_id'];
        krsort($region_array);
        $address_info = '';
        $region_info = [];
        foreach ($region_array as $k => $v)
        {
            $set[$key[$k]] = $v['id'];
            $address_info .= $v['name'];
            $region_info[]['region'] = ['id'=>$v['id'],'name'=>$v['name'],'pinyin'=>$v['pinyin'],'parentId'=>$v['parent_id']];
        }
        $set['address_info'] = $address_info.$address['street'];
        $set['region_info'] = json_encode($region_info,JSON_UNESCAPED_UNICODE);
        if($set['is_default']==2)
        {
            CisUserAddress::where("user_id",$user_id)->update(['is_default'=>0]);
        }
        if($id)
        {
            $bool = CisUserAddress::where('id', $id)->where('user_id',$user_id)->update($set);
        }
        else
        {
            $set['user_id'] = $user_id;
            $bool = CisUserAddress::create($set);
            $bool = $bool->id;
        }

       return $bool;
    }

    /**
     * 收货地址列表
     * @param $request object
     * @return mixed
     */
    public function getApiList(TableRequest $request,$user_id){

        $data = CisUserAddress::where('user_id',$user_id)
            ->orderBy($request->orderBy,$request->orderType)
            ->paginate($request->limit,['*'],'page',$request->page);
        foreach($data as $v) {
            $item = [];
            $item['id'] = $v->id;
            $item['name'] = $v->consignee;
            $item['mobile'] = $v->mobile;
            $item['isDefault'] = $v->is_default;
            $item['regionId'] = $v->region_id;
            $item['regionInfo'] = $v->region_info;
            $item['street'] = $v->street;
            $item['address'] = $v->address_info;
            $item['street'] = $v->street;
            $return['addresses'][] = $item;
        }
        $return['total'] =$data->total();
        return $return;
    }

    public function getDetail($id,$user_id)
    {
        if (!$id || !$user_id) {
            return false;
        }
        return CisUserAddress::where('id', $id)->where("user_id",$user_id)->first();
    }
}